###########################################
#  Primary Divisions: How Voters Evaluate Policy and Group Differences in Intra-Party Contests
#   - Forthcoming at The Journal of Politics
#   - Henderson et al 2021
#
###########################################
#  - code by S. Goggin & J. Henderson
########################################################
# This file produces results incorporate into Table 1 in the main draft; measures support by levels of consistency x agreement
########################################################
# inputs :: data/data_matrix_scored.Rdata
# 			 :: data/candidate_matrix_scored.csv

# outputs ::
# -- Table 1: 'Rate of Candidate Support by Levels of Agreement and Number of Party Consistent Positions'
# => main/figures/inparty_defect.tex

# -- Supplementary Results :: outparty results + many other tables


#dirs="~/Dropbox/replication0/"
#dirs should be set here or in runR.R 

rm(list=ls()[which(ls()!='dirs')])

library(ggplot2)
library(stringr)

# messy function to reorder by some estimate value
lableOrder=function(xmat,labels,label.groups,omits,o.column){

	# denote which label is to be omitted on the label
	for(i in 1:length(omits)){
		labels[which(labels==omits[i])]=paste('omit',labels[which(labels==omits[i])],sep='_')
	}

	# break groups into levels
	un_group=unique(label.groups)

	# this is the item to sort on, typically global or independent
	xm=xmat[,o.column]

	# vector which will contain row order
	xo=1:length(xm)

	# rearranging roworder within level
	for(j in 1:length(un_group)){
		ix=which(label.groups==un_group[j])
		if(length(ix)>2){
			ix=ix[!grepl(labels[ix],pattern='omit')]
			xo[ix]=xo[ix][order(xm[ix])]
		}
	}
	return(xmat[xo,])
}

reOrder=function(x,o){
	ix=array(NA,nrow(x))
	for(i in 1:length(o)){
		ix[i]=which(x$iv_order==o[i])
	}
	return(x[ix,])
}

setwd(dirs)

load('data/data_matrix_scored.Rdata')
scored=read.csv('data/candidate_matrix.csv')


###########################################
###First, need to stack based on candidates, not just candidate pairs (and also get text out for labels later)

#This has leaners as independents, which is incorrect
#cces_stacked$pid3clean <- ifelse(cces_stacked$pid3=="Democrat",-1,ifelse(cces_stacked$pid3=="Republican",1,0))

library(car)

###########################################
###Then, produce models w/ Clustered SEs

#Function from: http://scholar.byu.edu/jgubler/book/clustered-standard-errors-r
#Need this for clustered standard errors
clse.f <- function(dat,fm, cluster){
 require(sandwich)
 require(lmtest)
 not <- attr(fm$model,"na.action")
if( ! is.null(not)){
  cluster <- cluster[-not]
    dat <- dat[-not,]
}
 with(dat,{
 M <- length(unique(cluster))
 N <- length(cluster)
 K <- fm$rank
 dfc <- (M/(M-1))*((N-1)/(N-K))
 uj <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
 vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
 coeftest(fm, vcovCL)
 }
 )
}

data_matrix=data_matrix[,-c(grep(names(data_matrix),pattern='scores'))]
candidate_matrix=cbind(data_matrix,scored[,c('scores','scores_var','scores_policy','scores_policy_var')])

# i_policy is conjoint position in either i1 or i2
# consistency here is candidate too consistent Dem or Rep position on policy
# analogous to above variance in aggregating over candidate profiles on issues

candidate_matrix$consistency=
as.numeric(candidate_matrix$"i_lgbt"==1)*-1+
as.numeric(candidate_matrix$"i_marriage"==1)*1+
as.numeric(candidate_matrix$"i_need"==1)*-1+
as.numeric(candidate_matrix$"i_govabuse"==1)*1+
as.numeric(candidate_matrix$"i_freetrade"==1)*-1+
as.numeric(candidate_matrix$"i_unfairtrade"==1)*1+
as.numeric(candidate_matrix$"i_righttochoose"==1)*-1+
as.numeric(candidate_matrix$"i_unbornlives"==1)*1+
as.numeric(candidate_matrix$"i_raisetaxes"==1)*-1+
as.numeric(candidate_matrix$"i_cuttaxes"==1)*1+
as.numeric(candidate_matrix$"i_co2emissions"==1)*-1+
as.numeric(candidate_matrix$"i_drilling"==1)*1+
as.numeric(candidate_matrix$"i_citizenship"==1)*-1+
as.numeric(candidate_matrix$"i_bordersecurity"==1)*1+
as.numeric(candidate_matrix$"i_guncontrol"==1)*-1+
as.numeric(candidate_matrix$"i_gunrights"==1)*1+
as.numeric(candidate_matrix$"i_reducemilitary"==1)*-1+
as.numeric(candidate_matrix$"i_strengthenmilitary"==1)*1+
as.numeric(candidate_matrix$"i_policing"==1)*-1+
as.numeric(candidate_matrix$"i_criminals"==1)*1


candidate_matrix$libcon=rowMeans(cbind(candidate_matrix[,grep(names(candidate_matrix),pattern='libcon')],candidate_matrix$self_place/3))


############################################################
############################################################
###Now for Primary Elections (by PID)  -  InParty
############################################################
############################################################
###Now for Primary Elections (by PID)  -  OutParty

load('data/issueCrossPressureData.Rdata')

########################################################################
########################################################################
# matrix of two-way support by issue 1 and issue 2 by party
########################################################################
########################################################################

reps_agree=dems_agree=as.data.frame(matrix(NA,20,20))
ixa=c("need","govabuse",
"reducemilitary","strengthenmilitary",
"raisetaxes","cuttaxes",
"lgbt","marriage",
"co2emissions","drilling",
"freetrade","unfairtrade",
"righttochoose","unbornlives",
"guncontrol","gunrights",
"citizenship","bordersecurity",
"policing","criminals")

#ixb=c('libcon_need','libcon_dfns','libcon_taxs','libcon_gays','libcon_envs',
	#'libcon_trad','libcon_abrt','libcon_guns','libcon_immi','libcon_crme')


issue_mat=cbind(
		as.numeric(candidate_matrix$libcon_need<=0),
		as.numeric(candidate_matrix$libcon_need>=0),
		as.numeric(candidate_matrix$libcon_dfns<=0),
		as.numeric(candidate_matrix$libcon_dfns>=0),
		as.numeric(candidate_matrix$libcon_taxs<=0),
		as.numeric(candidate_matrix$libcon_taxs>=0),
		as.numeric(candidate_matrix$libcon_gays<=0),
		as.numeric(candidate_matrix$libcon_gays>=0),
		as.numeric(candidate_matrix$libcon_envs<=0),
		as.numeric(candidate_matrix$libcon_envs>=0),
		as.numeric(candidate_matrix$libcon_trad<=0),
		as.numeric(candidate_matrix$libcon_trad>=0),
		as.numeric(candidate_matrix$libcon_abrt<=0),
		as.numeric(candidate_matrix$libcon_abrt>=0),
		as.numeric(candidate_matrix$libcon_guns<=0),
		as.numeric(candidate_matrix$libcon_guns>=0),
		as.numeric(candidate_matrix$libcon_immi<=0),
		as.numeric(candidate_matrix$libcon_immi>=0),
		as.numeric(candidate_matrix$libcon_crme<=0),
		as.numeric(candidate_matrix$libcon_crme>=0)
)
colnames(issue_mat)=ixa
rownames(reps_agree)=rownames(dems_agree)=colnames(reps_agree)=colnames(dems_agree)=ixa


for(i in 1:20){
	for(j in i:20){
		dems_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid==-1),i]==1 & issue_mat[which(candidate_matrix$pid==-1),j]==1)
		reps_agree[i,j]=mean(issue_mat[which(candidate_matrix$pid== 1),i]==1 & issue_mat[which(candidate_matrix$pid== 1),j]==1)
	}
}

############################################################
# baseline agreement measures of issue-by-issue ideology
############################################################

library(xtable)
print(xtable(dems_agree),file='main/figures/demsIssueAgreeMatrix.tex')
print(xtable(reps_agree),file='main/figures/repsIssueAgreeMatrix.tex')

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# invote

reps <- subset(invote, invote$pid3==1)
dems <- subset(invote, invote$pid3==-1)
inds <- subset(invote, invote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)

reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

invote=rbind(dems,reps,inds)

############################################################
# inconsistency w/n primary and outparty comparisons
############################################################
# outvote

dems <- subset(outvote, outvote$pid3==1)
reps <- subset(outvote, outvote$pid3==-1)
inds <- subset(outvote, outvote$pid3==0)

dems$consistency1=as.numeric(dems$consistency1==-1)
dems$consistency2=as.numeric(dems$consistency2==-1)
reps$consistency1=as.numeric(reps$consistency1== 1)
reps$consistency2=as.numeric(reps$consistency2== 1)

inds$consistency1[which(inds$pty==1)]=as.numeric(inds$consistency1[which(inds$pty==1)]== 1)
inds$consistency2[which(inds$pty==1)]=as.numeric(inds$consistency2[which(inds$pty==1)]== 1)
inds$consistency1[which(inds$pty==0)]=as.numeric(inds$consistency1[which(inds$pty==0)]==-1)
inds$consistency2[which(inds$pty==0)]=as.numeric(inds$consistency2[which(inds$pty==0)]==-1)

reps$consistency=sign(reps$consistency)+1
dems$consistency=abs(sign(dems$consistency)-1)

inds$consistency[which(inds$pty==1)]=sign(inds$consistency[which(inds$pty==1)])+1
inds$consistency[which(inds$pty==0)]=abs(sign(inds$consistency[which(inds$pty==0)])-1)

outvote=rbind(dems,reps,inds)

invote=cbind(invote,'invote')
outvote=cbind(outvote,'outvote')
names(outvote)[ncol(outvote)]=names(invote)[ncol(invote)]='invoted'
invote[,ncol(invote)]=as.character(invote[,ncol(invote)])
outvote[,ncol(outvote)]=as.character(outvote[,ncol(outvote)])

vote=rbind(invote,outvote)

################################################################
# consistency pooling dems and reps
################################################################

ds=quantile(vote$libcon[which(vote$pid==-1)],pr=c(.75,.5))
rs=quantile(vote$libcon[which(vote$pid== 1)],pr=c(.25,.5))

vote$crossp25=NA
vote$crossp50=NA

vote$crossp25[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[1])
vote$crossp50[which(vote$pid==-1)]=as.numeric(vote$libcon[which(vote$pid==-1)]>ds[2])

vote$crossp25[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[1])
vote$crossp50[which(vote$pid== 1)]=as.numeric(vote$libcon[which(vote$pid== 1)]<rs[2])

###TBD predprob
#
vote$c0a0=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==0
vote$c0a1=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==1
vote$c0a2=vote$consistency1+vote$consistency2==0 & vote$agree1+vote$agree2==2

vote$c1a0=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==0
vote$c1a2=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==2

vote$c1a1=vote$consistency1+vote$consistency2==1 & vote$agree1+vote$agree2==1
## does agreement x constituency matter on the
# 1 0 | 0 1
# 0 1 | 1 0

# 1 0 | 1 0
# 0 1 | 0 1
vote$c1a1_align=(vote$consistency1==1 & vote$agree1==1 & vote$consistency2==0 & vote$agree2==0) | (vote$consistency1==0 & vote$agree1==0 & vote$consistency2==1 & vote$agree2==1)
vote$c1a1_misal=(vote$consistency1==0 & vote$agree1==1 & vote$consistency2==1 & vote$agree2==0) | (vote$consistency1==1 & vote$agree1==0 & vote$consistency2==0 & vote$agree2==1)

vote$c2a0=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==0
vote$c2a1=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==1
vote$c2a2=vote$consistency1+vote$consistency2==2 & vote$agree1+vote$agree2==2


attach(vote)
pred_prob_invote <- lm(dv_choice~
	#vote$c0a0+
	vote$c0a1+vote$c0a2+
	vote$c1a0+vote$c1a1+vote$c1a2+
	vote$c2a0+vote$c2a1+vote$c2a2,subset=which(vote$invoted=='invote'))
pred_prob_invote_clse <- clse.f(vote[which(vote$invoted=='invote'),],pred_prob_invote,respondent[which(vote$invoted=='invote')])


###
attach(vote)
# fixing 2 consistent positions :: co-party and out-party voters

weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==1 & consistency2==1)])
#0.570
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 & invoted=='invote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.443
weighted.mean(w=wt[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 & invoted=='invote' & consistency1==0 & consistency2==0)])
#0.304

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.597
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.520
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.444

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))


x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.347
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.327
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.203

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.461 #=> deviate from a consistent position in party primary, who expect to replace above loss on one deviation

# merge two-way split
# party consistent defectors
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.401 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.434 #=> deviate from a consistent position in party primary, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.369 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


defect_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(defect_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat)=c('agree 2','agree 1','agree 0')
defect_mat=cbind(defect_mat,c(y1,y2,y3))

# number of experimental units not clustered
rbind(c(n1,n2,n3),c(n4,n5,n6),c(n7,n8,n9))
#[,1] [,2] [,3]
#[1,] 6497 3296  637
#[2,] 2091 3900  764
#[3,]  171  473  339
#effective unit sizes
defect_mat_n=rbind(
	c(
		round((n1/nrow(invote))*length(unique(invote$respondent))),
		round((n2/nrow(invote))*length(unique(invote$respondent))),
		round((n3/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n4/nrow(invote))*length(unique(invote$respondent))),
		round((n5/nrow(invote))*length(unique(invote$respondent))),
		round((n6/nrow(invote))*length(unique(invote$respondent)))
	),
	c(
		round((n7/nrow(invote))*length(unique(invote$respondent))),
		round((n8/nrow(invote))*length(unique(invote$respondent))),
		round((n9/nrow(invote))*length(unique(invote$respondent)))
	)
)


rownames(defect_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(defect_mat_n)=c('agree 2','agree 1','agree 0')
#defect_mat_n=cbind(defect_mat_n,c(y1,y2,y3))
## exploring the outparty conditions

yy1=c(y1,y2,y3)

# above is just co-partisans
# below merges out-partisans

#
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.415
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.544
weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.654

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
n1=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)])
#0.523
x2=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
n2=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])
#0.449
x3=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
n3=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)])
#0.347

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
n7=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)])
#0.712
x8=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
n8=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))])
#0.586
x9=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
n9=length(dv_choice[which(pid3!=0 &invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)])
#0.535

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))


# happily split :: similar split/cross-pressure
x4=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
n4=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))])
#0.543 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=c(wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(c(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))

# unhappily split ::
x6=weighted.mean(w=wt[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
n6=length(dv_choice[which(pid3!=0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))])
#0.498 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


cross_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(cross_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat)=c('agree 2','agree 1','agree 0')
cross_mat=cbind(cross_mat,c(y1,y2,y3))

# expperimnetal units not clustered
rbind(c(n1,n2,n3),c(n4,n5,n6),c(n7,n8,n9))
#[,1] [,2] [,3]
#[1,]  764 2067 2267
#[2,]  810 2107  423
#[3,]  308  158   40

#effective unit sizes
cross_mat_n=rbind(
	c(
		round((n1/nrow(outvote))*length(unique(outvote$respondent))),
		round((n2/nrow(outvote))*length(unique(outvote$respondent))),
		round((n3/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n4/nrow(outvote))*length(unique(outvote$respondent))),
		round((n5/nrow(outvote))*length(unique(outvote$respondent))),
		round((n6/nrow(outvote))*length(unique(outvote$respondent)))
	),
	c(
		round((n7/nrow(outvote))*length(unique(outvote$respondent))),
		round((n8/nrow(outvote))*length(unique(outvote$respondent))),
		round((n9/nrow(outvote))*length(unique(outvote$respondent)))
	)
)


rownames(cross_mat_n)=c('consistent 2','consistent 1','consistent 0')
colnames(cross_mat_n)=c('agree 2','agree 1','agree 0')

yy2=c(y1,y2,y3)


#
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & consistency2==1 | pid3==0 & invoted=='outvote' & consistency1==1 & consistency2==1)])
#0.415
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & consistency2==0 | pid3==0 & consistency1==0 & consistency2==1) | invoted=='outvote' & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
#0.544
weighted.mean(w=wt[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)],dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & consistency2==0 | pid3==0 & invoted=='outvote' & consistency1==0 & consistency2==0)])
#0.654

# consistent positions drop off less among those who disagree w/n party
x1=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]),
c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],
  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
n1=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==1 & consistency2==1 & agree2==1)]))
#0.523
x2=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
		wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]), #consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],
  	dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))])) #consistency2==1 & agree2==1))]))
n2=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==1 & agree2==0 | consistency1==1 & agree1==0 & consistency2==1 & agree2==1))]))
#0.449
x3=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
n3=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==1 & agree1==0 & consistency2==1 & agree2==0)]))
#0.347

y1=sum((c(n1,n2,n3)/sum(c(n1,n2,n3)))*c(x1,x2,x3))

x7=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
n7=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==1 & consistency2==0 & agree2==1)]))
#0.712
x8=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
n8=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==0 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==0 & agree2==1))]))
#0.586
x9=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],wt[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],
	  dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
n9=length(c(dv_choice[which(pid3==0 & invoted=='invote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)],dv_choice[which(pid3==0 & invoted=='outvote' & consistency1==0 & agree1==0 & consistency2==0 & agree2==0)]))
#0.535

y3=sum((c(n7,n8,n9)/sum(c(n7,n8,n9)))*c(x7,x8,x9))

# happily split :: similar split/cross-pressure
x4=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]),
	c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],
	  dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
n4=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))],dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==1))]))
#0.543 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

# party consistent defectors
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))])
#0.600 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

# moderate splits
#mean(dv_choice[which(invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))])
#0.546 #=> deviate from a consistent position in party invote, who expect is first to go on one deviation

x5=weighted.mean(w=
	c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
	wt[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
n5=length(
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==1 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==1))],
					dv_choice[which(pid3==0 & invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==1 | consistency1==0 & agree1==1 & consistency2==1 & agree2==0))]))
#

# unhappily split ::
x6=weighted.mean(w=c(wt[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],wt[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]),
				c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],
					dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
n6=length(c(dv_choice[which(pid3==0 & invoted=='invote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))],dv_choice[which(pid3==0 &invoted=='outvote' & (consistency1==1 & agree1==0 & consistency2==0 & agree2==0 | consistency1==0 & agree1==0 & consistency2==1 & agree2==0))]))
#0.498 #=> deviate from a consistent position in party invote, who expect to replace above loss on one deviation

y2=sum((c(n4,n5,n6)/sum(c(n4,n5,n6)))*c(x4,x5,x6))


indep_mat=rbind(c(x1,x2,x3),c(x4,x5,x6),c(x7,x8,x9))
rownames(indep_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(indep_mat)=c('agree 2','agree 1','agree 0')
indep_mat=cbind(indep_mat,c(y1,y2,y3))

yy3=c(y1,y2,y3)

p1=1
p2=0
p3=0
#2 to 1; out to ind
pt=1e+09
while((yy1*p1+yy2*p2+yy3*p3)[1]>(yy1*p1+yy2*p2+yy3*p3)[3]){
	p1=p1-.01
	p2=(1-p1)*pt/(pt+1)
	p3=(1-p1)*1/(pt+1)
}

p1
#0.46
p2
#0.54
p3
#0

# brief CQ exploration is not really
# by CQ?
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==1 & consistency2==1)])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==1 & consistency2==1)])
#0.563

weighted.mean(w=wt[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq==1 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & (consistency1==1 & consistency2==0 | consistency1==0 & consistency2==1))])

#0.420
weighted.mean(w=wt[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq==1 & consistency1==0 & consistency2==0)])
weighted.mean(w=wt[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)],dv_choice[which(invoted=='invote' & cq>1 & cq<4 & consistency1==0 & consistency2==0)])
#0.285


#replicate using a aggregate measure of cross-pressure,
# a) above agreement measures could have error which distorts measures of support
# b) use summary score, i.e., the average of the libcon measures to decide 'cross-pressure'
#  - set threshold at i) 25% and ii) 50% of party supporters

x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==1)])
#0.5100464
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp25==0)])
#0.5808467

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==1)])
#0.4745342
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp25==0)])
#0.4025267

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==1)])
#0.4633028
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp25==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp25==0)])
# 0.2339869

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp25_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp25_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp25_mat)=c('not cross','cross')
cp25_mat=cbind(cp25_mat,c(y1,y2,y3))

yy1=c(y1,y2,y3)



x1=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
n1=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==1)])
#0.5255489
x2=weighted.mean(w=wt[which(invoted=='invote' & consistency==2 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
n2=length(dv_choice[which(invoted=='invote' & consistency==2 & crossp50==0)])
#0.6000379

y1=sum((c(n1,n2)/sum(c(n1,n2)))*c(x1,x2))

x3=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
n3=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==1)])
#0.4633999
x4=weighted.mean(w=wt[which(invoted=='invote' & consistency==1 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
n4=length(dv_choice[which(invoted=='invote' & consistency==1 & crossp50==0)])
#0.3777907

y2=sum((c(n3,n4)/sum(c(n3,n4)))*c(x3,x4))

x5=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==1)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
n5=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==1)])
#0.3604888
x6=weighted.mean(w=wt[which(invoted=='invote' & consistency==0 & crossp50==0)],dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
n6=length(dv_choice[which(invoted=='invote' & consistency==0 & crossp50==0)])
#0.2093496

y3=sum((c(n5,n6)/sum(c(n5,n6)))*c(x5,x6))


cp50_mat=rbind(c(x2,x1),c(x4,x3),c(x6,x5))
rownames(cp50_mat)=c('consistent 2','consistent 1','consistent 0')
colnames(cp50_mat)=c('not cross','cross')
cp50_mat=cbind(cp50_mat,c(y1,y2,y3))

yy2=c(y1,y2,y3)

print(xtable(defect_mat,digits=3),file='main/figures/inparty_defect.tex')
#> defect_mat
# OLDS
#               agree 2   agree 1   agree 0
#consistent 2 0.5969897 0.5196586 0.4435737 0.5632790
#consistent 1 0.4609190 0.4070184 0.3689189 0.4196891
#consistent 0 0.3468208 0.3266667 0.2027778 0.2848423

#agree 2   agree 1   agree 0
#consistent 2 0.6104837 0.5170824 0.4329913 0.5701276
#consistent 1 0.4783666 0.4249450 0.4120150 0.4400192
#consistent 0 0.3658363 0.3251245 0.2253521 0.2977988

print(xtable(cross_mat,digits=3),file='main/figures/outparty_defect.tex')
#> cross_mat
# OLDS
#               agree 2   agree 1   agree 0
#consistent 2 0.5231788 0.4494005 0.3467671 0.4148686
#consistent 1 0.5431034 0.5539637 0.4976959 0.5440120
#consistent 0 0.7123288 0.5847953 0.5348837 0.6541502

#agree 2   agree 1   agree 0
#consistent 2 0.5302433 0.4505220 0.3456447 0.4158320
#consistent 1 0.5945423 0.5490106 0.4835633 0.5275125
#consistent 0 0.7162726 0.5784672 0.5165413 0.6653585


print(xtable(indep_mat,digits=3),file='main/figures/indep_defect.tex')
#> indep_mat
#               agree 2   agree 1   agree 0
#consistent 2 0.5789183 0.4621554 0.4026591 0.4927984
#consistent 1 0.5532995 0.4515187 0.4046512 0.4771346
#consistent 0 0.5582822 0.4306931 0.3333333 0.4622426

print(xtable(cp25_mat,digits=3),file='main/figures/cp25_defect.tex')
#> cp25_mat
#not cross     cross
#consistent 2 0.5808467 0.5100464 0.5632790
#consistent 1 0.4025267 0.4745342 0.4196891
#consistent 0 0.2339869 0.4633028 0.2848423

print(xtable(cp50_mat,digits=3),file='main/figures/cp50_defect.tex')
#> cp50_mat
#not cross     cross
#consistent 2 0.6000379 0.5255489 0.5632790
#consistent 1 0.3777907 0.4633999 0.4196891
#consistent 0 0.2093496 0.3604888 0.2848423

#END
